<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path: '/home'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-button type="primary" @click="addDialogVisible=true">添加角色</el-button>
      <!-- 表格区域 -->
      <el-table border stripe :data="rolesList" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 一级权限 -->
            <el-row v-for="(itemOne, indexOne) in scope.row.children" :key="itemOne.id" :class="['borderBottom',indexOne === 0 ? 'borderTop' : '']">
              <el-col :span="4">
                <el-tag closable @close="removeRights(scope.row, itemOne.id)">{{itemOne.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="20">
                <!-- 二级权限 -->
                <el-row v-for="(itemTwo, indexTwo) in itemOne.children" :key="itemTwo.id" :class="indexTwo === 0 ? '' : 'borderTop'">
                  <el-col :span="5">
                    <el-tag type="success" closable @close="removeRights(scope.row, itemTwo.id)">{{itemTwo.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="19">
                    <el-tag type="warning" v-for="itemThree in itemTwo.children" :key="itemThree.id" closable @close="removeRights(scope.row, itemThree.id)">{{itemThree.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="120"></el-table-column>
        <el-table-column prop="roleDesc" label="描述"></el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope)">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRightDialog(scope.row)">分配权限</el-button>

          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加角色对话框 -->
    <el-dialog
      title="添加角色"
      :visible.sync="addDialogVisible"
      width="50%" @close="addDialogClosed">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑角色对话框 -->
    <el-dialog
      title="编辑角色"
      :visible.sync="editDialogVisible"
      width="50%" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRole">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%">
      <!-- 树形控件 -->
      <!-- :data 指定要绑定的数据 -->
      <!-- :props 指定每个节点的配置项 -->
      <!--  label 指定展示的名称 -->
      <!--  children 指定哪个属性实现嵌套 -->
      <!-- node-key 指定节点被选中时,选中的值 -->
      <el-tree :data="rightTree" :props="treeProps" show-checkbox :default-expand-all="true" :default-checked-keys="defaultCheckedLeftKeys" node-key="id" ref="tree"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import mix from './Roles-mixins.js'
export default {
  mixins: [mix]
}
</script>
<style lang="less" scoped>
.borderTop {
  border-top: 1px solid #ddd;
}
.borderBottom {
  border-bottom: 1px solid #ddd;
}
.el-tag {
  margin: 10px 5px;
}
.el-col {
  white-space: nowrap;
}
.el-col-19 {
  white-space: normal;
}
</style>
